package com.fenghongzhang.day011;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Telephony;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class Main2Activity extends AppCompatActivity {
    private EditText edit;

    private ContentResolver contentResolver;

    private SMSOb smsOb;
    private Handler handler = new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String s = (String) msg.obj;

            edit.setText(s);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        edit = (EditText) findViewById(R.id.edit);

        //1, 注册观察者
         contentResolver = getContentResolver();

        smsOb = new SMSOb(handler,contentResolver);

        contentResolver.registerContentObserver(Telephony.Sms.CONTENT_URI,true,smsOb);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        contentResolver.unregisterContentObserver(smsOb);
    }
}
